﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using RanchBuddy.Core.Services.Impl;
using StructureMap;

namespace RanchBuddy.Core.Services.Impl
{
    [Pluggable("Default")]
    public class ImageService : IImageService
    {
        public static void Resize(HttpPostedFileBase F, string SaveToFolder, string SystemFileNamePrefix, string Extension, List<FileSizeAndIdentifier> sizeAndIdentifiers)
        {
            int NewWidth = 0;
            int NewHeight = 0;
            //Makes all the different sizes in the sizesToMake collection
            foreach (FileSizeAndIdentifier fsai in sizeAndIdentifiers)
            {
                using (System.Drawing.Image image = System.Drawing.Image.FromStream(F.InputStream))
                    //determine the thumbnail sizes
                using (Bitmap bitmap = new Bitmap(image))
                {
                    decimal Ratio;
                    if (bitmap.Width > bitmap.Height)
                    {
                        Ratio = (decimal)fsai.FileSize / bitmap.Width;
                        NewWidth = fsai.FileSize;
                        decimal Temp = bitmap.Height * Ratio;
                        NewHeight = (int)Temp;
                    }
                    else
                    {
                        Ratio = (decimal)fsai.FileSize / bitmap.Height;
                        NewHeight = fsai.FileSize;
                        decimal Temp = bitmap.Width * Ratio;
                        NewWidth = (int)Temp;
                    }
                }

                using (System.Drawing.Image image = System.Drawing.Image.FromStream(F.InputStream))
                using (Bitmap bitmap = new Bitmap(image, NewWidth, NewHeight))
                {
                    bitmap.Save(SaveToFolder + "/" + SystemFileNamePrefix + "_" + fsai.FileNameIdentifier + "." + Extension, image.RawFormat);
                }
            }
        }

        public static bool VerifyPath(string Path)
        {
            bool result = false;
            try
            {
                if (!Directory.Exists(Path))
                    Directory.CreateDirectory(Path);
                result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }

        public static void DeleteFilesByFileSystemPrefix(string FileSystemPrefix, string Folder)
        {
            if (Directory.Exists(Folder))
            {
                string[] imgList = Directory.GetFiles(Folder,FileSystemPrefix);
                foreach (string img in imgList)
                {
                    FileInfo imgInfo = new FileInfo(img);
                    imgInfo.Delete();
                }
            }

        }

        public static List<FileSizeAndIdentifier> GetStandardSizesAndIdentifiers()
        {
            List<FileSizeAndIdentifier> sizeAndIdentifiers = new List<FileSizeAndIdentifier>();
            FileSizeAndIdentifier thumbnail = new FileSizeAndIdentifier();
            FileSizeAndIdentifier small = new FileSizeAndIdentifier();
            FileSizeAndIdentifier medium = new FileSizeAndIdentifier();
            FileSizeAndIdentifier large = new FileSizeAndIdentifier();

            thumbnail.FileSize = ConfigurationService.GetImageWidthThumbnail();
            thumbnail.FileNameIdentifier = "T";
            sizeAndIdentifiers.Add(thumbnail);

            small.FileSize = ConfigurationService.GetImageWidthSmall();
            small.FileNameIdentifier = "S";
            sizeAndIdentifiers.Add(small);

            medium.FileSize = ConfigurationService.GetImageWidthMedium();
            medium.FileNameIdentifier = "M";
            sizeAndIdentifiers.Add(medium);

            large.FileSize = ConfigurationService.GetImageWidthLarge();
            large.FileNameIdentifier = "L";
            sizeAndIdentifiers.Add(large);

            return sizeAndIdentifiers;
        }

        public struct FileSizeAndIdentifier
        {
            public int FileSize { get; set; }
            public string FileNameIdentifier { get; set; }
        }
    }
}